package dhm.com.hpx.activity;

        import android.content.ClipData;
        import android.content.ClipboardManager;
        import android.content.Context;
        import android.content.Intent;
        import android.graphics.Bitmap;
        import android.graphics.drawable.BitmapDrawable;
        import android.support.v7.widget.LinearLayoutManager;
        import android.view.Gravity;
        import android.view.View;
        import android.view.ViewGroup;
        import android.view.WindowManager;
        import android.widget.PopupWindow;
        import android.widget.TextView;
        import android.widget.Toast;

        import com.facebook.drawee.view.SimpleDraweeView;
        import com.jcodecraeer.xrecyclerview.XRecyclerView;

        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;

        import butterknife.BindView;
        import butterknife.ButterKnife;
        import butterknife.OnClick;
        import dhm.com.hpx.R;
        import dhm.com.hpx.adapter.RecommendAdapter;
        import dhm.com.hpx.base.BaseActivity;
        import dhm.com.hpx.base.Presenter.PressenterImpl;
        import dhm.com.hpx.base.netWork.Constant;
        import dhm.com.hpx.base.netWork.LoginContract;
        import dhm.com.hpx.entity.GetRecommendBean;
        import dhm.com.hpx.utils.SpUtils;
        import dhm.com.hpx.utils.ZxingUtils;

public class MyrecommendActivity extends BaseActivity implements LoginContract.IView {

    @BindView(R.id.recommend_code)
    TextView recommendCode;
    @BindView(R.id.image)
    SimpleDraweeView image;
    @BindView(R.id.num)
    TextView num;
    @BindView(R.id.vipnum)
    TextView vipnum;
    @BindView(R.id.recy)
    XRecyclerView recy;
    private String uid;
    private PressenterImpl pressenter;
    private List<GetRecommendBean.DataBean.UserListBean> userList;
    private RecommendAdapter adapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_myrecommend;
    }

    private int page = 1;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        uid = SpUtils.getString(this, "uid");

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recy.setLayoutManager(manager);

        adapter = new RecommendAdapter(this);
        recy.setAdapter(adapter);

        recy.setLoadingMoreEnabled(true);
        recy.setPullRefreshEnabled(true);

        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getdata();
            }

            @Override
            public void onLoadMore() {
                getdata();
            }
        });

        getdata();
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    private void getdata() {
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid", uid);
        map.put("page", page + "");
        pressenter.sendMessage(this, Constant.recommend_user_list, map, GetRecommendBean.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void requesta(Object data) {
        if (data instanceof GetRecommendBean) {
            GetRecommendBean getRecommendBean = (GetRecommendBean) data;
            if (getRecommendBean.getCode() == 1) {
                if (getRecommendBean.getData().getRecommend_code().equals("")){
                    Toast.makeText(this, "您还不是推荐人，请申请！", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(MyrecommendActivity.this,AddrecommendActivity.class);
                    startActivity(intent);
                    finish();
                    return;
                }

                Bitmap qrCode = ZxingUtils.createQRCode(Constant.PATH+"appapi/appletscode/my_invitation/uid/"+uid);
                image.setImageBitmap(qrCode);
                showPopwindow(qrCode);

                recommendCode.setText(getRecommendBean.getData().getRecommend_code() + "");
                num.setText(getRecommendBean.getData().getRecommend_num() + "");
                vipnum.setText(getRecommendBean.getData().getRecommend_vipnum() + "");
                if (page == 1) {
                    userList = getRecommendBean.getData().getUser_list();
                } else {
                    userList.addAll(getRecommendBean.getData().getUser_list());
                }
                adapter.setList(userList);
                page++;
            }
            recy.loadMoreComplete();
            recy.refreshComplete();
        }
    }

    @Override
    public void fail(String error) {

    }

    @OnClick({R.id.back, R.id.copy, R.id.image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.image:
                setBackgroundAlpha(0.5f);
                popWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
                break;
            case R.id.copy:
                //获取剪贴版
                ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
//创建ClipData对象
//第一个参数只是一个标记，随便传入。
//第二个参数是要复制到剪贴版的内容
                ClipData clip = ClipData.newPlainText(null, recommendCode.getText().toString());
//传入clipdata对象.
                clipboard.setPrimaryClip(clip);
                Toast.makeText(this, "复制成功", Toast.LENGTH_SHORT).show();
                break;
            default:
        }
    }


    private PopupWindow popWindow;

    private void showPopwindow(Bitmap qrCode) {
        View popView = View.inflate(this, R.layout.dialog_image, null);
        TextView sure = popView.findViewById(R.id.sure);
        SimpleDraweeView image = popView.findViewById(R.id.image);
        image.setImageBitmap(qrCode);

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popWindow.dismiss();
            }
        });

        popWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popWindow.setFocusable(true);
        popWindow.setTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        popWindow.setOutsideTouchable(true);// 设置允许在外点击消失


        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // popupWindow隐藏时恢复屏幕正常透明度
                setBackgroundAlpha(1.0f);
            }
        });

    }


    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }



}
